//
//  DHDianpingListController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHDianpingListController.h"
#import "DHHomeComments_Cell.h"
#import "DHArticleNO_CommentCell.h"
#import "DHNewsDetailController.h"

@interface DHDianpingListController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArr;

@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation DHDianpingListController

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"点评列表";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHHomeComments_Cell" bundle:nil] forCellReuseIdentifier:kDHHomeComments_Cell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHArticleNO_CommentCell" bundle:nil] forCellReuseIdentifier:kDHArticleNO_CommentCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDianping)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDianping)];
    
    [self.tableView.mj_header beginRefreshing];
}

// 点评
- (void)loadDianping{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSDictionary *params = @{@"pageIndex":@(self.pageIndex),
                             @"pageSize":@(20),
                             @"brand_id":self.brandID
                             };
    [[DHNetAPIClient shareDHNetAPIClient] request_articles_brand_remarkWithDictionary:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.itemArr removeAllObjects];
        [self.itemArr addObjectsFromArray:[data objectForKey:@"remarks"]];
        if (self.itemArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

- (void)loadMoreDianping{
    NSDictionary *params = @{@"pageIndex":@(self.pageIndex),
                             @"pageSize":@(20),
                             @"brand_id":self.brandID
                             };
    [[DHNetAPIClient shareDHNetAPIClient] request_articles_brand_remarkWithDictionary:params andBlock:^(id data, NSError *error) {
        [self.itemArr removeAllObjects];
        [self.itemArr addObjectsFromArray:[data objectForKey:@"remarks"]];
        if (self.itemArr.count < 20 * (self.pageIndex + 1)) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        self.pageIndex += 1;
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.itemArr.count == 0) {
        return 1;
    }else{
        return self.itemArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.itemArr.count == 0) {
        return 40;
    }else{
        return 95;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.itemArr.count > 0) {
        DHHomeComments_Cell *cell = [tableView dequeueReusableCellWithIdentifier:kDHHomeComments_Cell forIndexPath:indexPath];
        NSDictionary *dic = self.itemArr[indexPath.row];
        [cell.imgView dh_setImageURLString:[dic objectForKey:@"cover"]];
        [cell.headImageView dh_setImageURLString:[[dic objectForKey:@"owner"] objectForKey:@"photo"]];
        cell.titleLabel.text = [[dic objectForKey:@"owner"] objectForKey:@"nickname"];
        cell.nameLabel.text = [[dic objectForKey:@"owner"] objectForKey:@"title"];
        cell.summaryLabel.text = [dic objectForKey:@"summary"];
        return cell;
    }else{
        DHArticleNO_CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHArticleNO_CommentCell forIndexPath:indexPath];
        cell.infoLabel.text = @"暂无点评";
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.itemArr[indexPath.row];
    DHNewsDetailController *vc = [[DHNewsDetailController alloc] initWithArticleID:[dic objectForKey:@"_id"]];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
